package org.myy.learn.hot.链表.环形链表;

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while (true){
            if (fast == null || fast.next == null){return null;}
            slow = slow.next;
            fast = fast.next.next;
            //比较节点即可，为什么比较fast.val呢。这里的fast可能是null的
            if (slow == fast){break;}
        }
        //存在环
        ListNode pre = head;
        while (pre != slow){
            slow = slow.next;
            pre = pre.next;
        }
        return pre;
    }
}
